import { defHttp } from '/@/utils/http/axios';
import { getMenuListResultModel } from './model/menuModel';
enum Api {
	// GetMenuList = 'Roles/permissions/resources',
	GetMenuList = 'Roles/permissions/resources',
	SwitchVue3Menu = '/sys/switchVue3Menu',
}

/**
 * @description: Get user menu based on id
 */

export const getMenuList = () => {
	return new Promise((resolve) => {
		//为了兼容mock和接口数据
		defHttp.get<getMenuListResultModel>({ url: Api.GetMenuList }).then((res) => {
			if (Array.isArray(res.data)) {
				let resp = res.data
				itemMeta(resp)
				resolve(resp);
			} else {
				resolve(res['menu']);
			}
		});

	});
};

function itemMeta(arr: any) {
	for (let i = 0; i < arr.length; i++) {
		if (arr[i].children.length > 0) {
			itemMeta(arr[i].children)
		}
		arr[i] = {
			...arr[i],
			path: arr[i].url || '',
			title: arr[i].name,
			meta: {
				...arr[i].meta,
				title: arr[i].name,
				name: arr[i].name,
				icon: arr[i].icon,
			}
		}
	}
}

/**
 * 切换成vue3菜单
 */
export const switchVue3Menu = () => {
	return new Promise((resolve) => {
		defHttp.get({ url: Api.SwitchVue3Menu });
	});
};
